package com.onetech.adrewards.free.activities;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.analytics.tracking.android.EasyTracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.dialogs.ExitAppDialog;
import com.onetech.adrewards.free.dialogs.LoginBonusDialog;
import com.onetech.adrewards.free.dialogs.ReviewAppSuccessDialog;
import com.onetech.adrewards.free.fragments.ExchangeFragmentNew;
import com.onetech.adrewards.free.fragments.GetPointsFragmentNewApi;
import com.onetech.adrewards.free.fragments.ListNewsFragment;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.viewpagerindicator.IconPagerAdapter;

public class HomeActivity extends BaseActivity{
	//private ImageView mHomeBanner=null;
//	private View menuBottomLeft=null;
//	private View menuBottomMiddle=null;
//	private View menuBottomRight=null;
//	private int currentView=R.id.menuLeft;
	public ViewPager mViewPager=null;
	private FragmentPagerAdapter mAdapter=null;
	//private TabPageIndicator mPagerIndicator=null;
	//private QuickAction mFirstMenu=null;
	public boolean isReviewApp=false;
	public String reviewSuccessMessage = "";
	
	private static final String[] CONTENT = new String[] { "Appli", "Recommend", "Free"};
	//private static final String[] CONTENT = new String[] { "", "", "", "" };
    private static final int[] ICONS = new int[] {
    	R.drawable.perm_group_calendar,
        R.drawable.perm_group_camera,
        R.drawable.perm_group_device_alarms,
    };
    public interface ExitHandler{
    	public void doExit();
    }

    private ExitHandler mAdHandler=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        startNotifyAppLaunch();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
        	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        }
        setMainView(R.layout.home_activity_layout);
        initView();
        initListener();
        if(AppConfig.LOGIN_BONUS){
        	LoginBonusDialog mLoginBonusDialog=new LoginBonusDialog(this);
        	mLoginBonusDialog.show();
        }
        mAdHandler=new ExitHandler() {			
			@Override
			public void doExit() {
				finish();				
			}
		};
    }
    
    private void startNotifyAppLaunch(){
    	AdrewadsUtils.checkUId();
//    	CARController.appkey = AppConfig.appKey;
//        //CARController.cid = "cid";
//    	CARController.cid = AppConfig.uId;
//        CARController.pid = "1";
//        CARController.mcwait = false;
//        CARController.nor = 1;
//        CARController.cpi = "1";
//        CARController.analytics = false;
//        CARController.notifyAppLaunch(this.getApplicationContext(), this.getIntent()); 
    }
    private void initView(){
    	mAdapter = new MyFragementAdapter(getSupportFragmentManager());

    	mViewPager = (ViewPager)findViewById(R.id.pager);
    	mViewPager.setAdapter(mAdapter);
    	currentView=R.id.menuMiddle;
    	switchLayout(currentView);
    	mViewPager.setCurrentItem(1);
    	
//        mPagerIndicator = (TabPageIndicator)findViewById(R.id.indicator);
//        mPagerIndicator.setViewPager(mViewPager);
//    	mPagerIndicator.setCurrentItem(0);
    	
//    	menuBottomLeft=(LinearLayout)findViewById(R.id.menuLeft);
//    	menuBottomMiddle=(LinearLayout)findViewById(R.id.menuMiddle);
//    	menuBottomRight=(LinearLayout)findViewById(R.id.menuRight);
    	
    	//setDefaultMenuBackground();
    	
//    	mFirstMenu=new QuickAction(this);
//    	mFirstMenu.setOnActionItemClickListener(this);
//    	setQuickAction(mFirstMenu, R.drawable.ic_launcher);
    }
    private void initListener(){
    	//mHomeBanner.setOnClickListener(this);
    	
//    	menuBottomLeft.setOnClickListener(this);
//    	menuBottomMiddle.setOnClickListener(this);
//    	menuBottomRight.setOnClickListener(this);
    	
    	mViewPager.setOnPageChangeListener(this);
    }
    @Override
    public void onClick(View v) {
    	super.onClick(v);
    	switchCurrentItemByViewId(currentView);
    }
    @Override
	public void onPageScrollStateChanged(int arg0) {
		
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
	}
	@Override
	public void onPageSelected(int position) {
		switchCurrentItem(position);
		switchLayout(currentView);
	}
	private void switchCurrentItemByViewId(int resId) {
		switch (resId) {
		case R.id.menuLeft:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.menuMiddle:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.menuRight:
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}
	public void switchCurrentItem(int position) {
		switch (position) {
		case 0:
			currentView=R.id.menuLeft;
			mViewPager.setCurrentItem(0);
			break;
		case 1:
			currentView=R.id.menuMiddle;
			mViewPager.setCurrentItem(1);
			break;
		case 2:
			currentView=R.id.menuRight;
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}
    class MyFragementAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
        public MyFragementAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	switch (position) {
    		case 0:
    			return Fragment.instantiate(getApplicationContext(), ListNewsFragment.class.getName());
    		case 1:
    			return Fragment.instantiate(getApplicationContext(), GetPointsFragmentNewApi.class.getName());
    		case 2:
    			return Fragment.instantiate(getApplicationContext(), ExchangeFragmentNew.class.getName());
    		default:
    			return Fragment.instantiate(getApplicationContext(), ListNewsFragment.class.getName());
    		}
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override public int getIconResId(int index) {
          return ICONS[index];
        }

      @Override
        public int getCount() {
          return CONTENT.length;
        }
    }
	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		try {
			ExitAppDialog mExitAppDialog=new ExitAppDialog(this,mAdHandler);
			mExitAppDialog.show();
		} catch (Exception e) {
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		getUserInfo();
		checkReviewApp();
	}
	
	
	private void checkReviewApp() {
		if(isReviewApp){
			isReviewApp=false;
		
			AdrewadsUtils.setStore(getApplicationContext(), Cache.REVIEW_APP_KEY, "Done");
			if(reviewSuccessMessage != null && reviewSuccessMessage.trim().length()>0){
				ReviewAppSuccessDialog dialog=new ReviewAppSuccessDialog(HomeActivity.this, getString(R.string.app_name), reviewSuccessMessage);
				dialog.show();
			}
		}
	}
	public void loadUserInfo(){
		getUserInfo();
	}
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {		
//		if (keyCode == KeyEvent.KEYCODE_BACK){
//			ExitAppDialog mExitAppDialog=new ExitAppDialog(this);
//			mExitAppDialog.show();
//		}
//		return super.onKeyDown(keyCode, event);
//	}
//	private void setDefaultMenuBackground(){
//		menuBottomLeft.setBackgroundResource(R.drawable.menu_bottom_background_selected);
//		menuBottomMiddle.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//		menuBottomRight.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//	}
//	private void switchLayout(int id){
//		switch (id) {
//		case R.id.menuLeft:
//			menuBottomLeft.setBackgroundResource(R.drawable.menu_bottom_background_selected);
//			menuBottomMiddle.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			menuBottomRight.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			mPagerIndicator.setCurrentItem(0);
//			break;
//		case R.id.menuMiddle:
//			menuBottomLeft.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			menuBottomMiddle.setBackgroundResource(R.drawable.menu_bottom_background_selected);
//			menuBottomRight.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			mPagerIndicator.setCurrentItem(1);
//			break;
//		case R.id.menuRight:
//			menuBottomLeft.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			menuBottomMiddle.setBackgroundResource(R.drawable.menu_bottom_background_normal);
//			menuBottomRight.setBackgroundResource(R.drawable.menu_bottom_background_selected);
//			mPagerIndicator.setCurrentItem(2);
//			break;
//		default:
//			break;
//		}		
//	}	
	@Override
	public void onStart() {
	    super.onStart();
	   // The rest of your onStart() code.
	    try {
	    	EasyTracker.getInstance(this).activityStart(this);  // Add this method.
		} catch (Exception e) {			
		}
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    // The rest of your onStop() code.
	    try {
	    	EasyTracker.getInstance(this).activityStop(this);  // Add this method.
		} catch (Exception e) {
		}
	  }
     
}
